package com.rcdz.mykotlindemo.designtest.fragment

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.bumptech.glide.Glide
import com.rcdz.mykotlindemo.R
import com.rcdz.mykotlindemo.model.bean.NewsBean
import com.rcdz.mykotlindemo.persenter.GetNewsInfo
import com.rcdz.mykotlindemo.persenter.NetPersenter
import com.wjt.mylibrary.base.BaseFragment
import kotlinx.android.synthetic.main.fragment_main.*

class MainFragment : BaseFragment(), GetNewsInfo {
    internal var adapter: MyAdapter? = null
    val items = arrayListOf<NewsBean.StoriesBean>()
    override fun lazyInit() {
    }

    override fun setNowActivityName(): String {
        return "MainFragment"
    }

    override fun setLayout(): Int {
        return R.layout.fragment_main;
    }

    override fun initView() {

    }

    override fun initData() {
        //创建适配器  并传入模拟的数据
        adapter = MyAdapter(items)
        //设置显示格式 2列
        //设置显示格式 2列
        val layoutParams = LinearLayoutManager(activity)
        //将显示格式传给mRecyclerView
        //将显示格式传给mRecyclerView
        datalist.setLayoutManager(layoutParams)
        //设置适配器
        //设置适配器
        datalist.setAdapter(adapter)
        val net = NetPersenter(activity);
        net.getNewsInfo(this)
        srl_activity_main.setColorSchemeResources(R.color.colorPrimary)
        //设置下拉属性监听器
        //设置下拉属性监听器
        srl_activity_main.setOnRefreshListener(SwipeRefreshLayout.OnRefreshListener {
            //调用initData方法获取数据
            net.getNewsInfo(this)
        })
    }

    override fun onClick(v: View?) {
        TODO("Not yet implemented")
    }

    //得到数据
    override fun getNewsInfo(newsBean: NewsBean?) {
        Log.i("test", "getNewsInfo")
        items.clear()
        newsBean?.stories?.let { items.addAll(it) } //添加数据
        adapter?.notifyDataSetChanged()
        //关闭刷新提示
        srl_activity_main.setRefreshing(false);
    }
}

internal class MyAdapter(private val mLists: ArrayList<NewsBean.StoriesBean>) :
    RecyclerView.Adapter<MyAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view: View = LayoutInflater.from(parent.context).inflate(
            R.layout.layout_item,
            parent, false
        )
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.mTextView.text = mLists.get(position).title
        val into: Any = Glide.with(holder.itemView.context)
            .load(mLists.get(position).images!!.get(0))
            .into(holder.mImageView)
    }

    override fun getItemCount(): Int {
        return mLists.size
    }

    internal class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val mTextView: TextView
        val mImageView: ImageView

        init {
            mTextView = itemView.findViewById(R.id.tv_layout_item)
            mImageView = itemView.findViewById(R.id.item_img)
        }
    }
}
